
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// keep alive
//https://blog.csdn.net/weixin_42108319/article/details/129078998

class ChatPages extends StatefulWidget {
  const ChatPages({Key? key}) : super(key: key);

  @override
  State<ChatPages> createState() => _ChatPagesState();
}

class _ChatPagesState extends State<ChatPages>{

  Drag? _drag;
  PageController pageController = PageController();
  PageController subPageController = PageController();
  Offset? localPosition;
  int ? downMicrosecond;
  DragStartDetails? currentDragStartDetails;
  DragUpdateDetails? lastDragUpdateDetails;
  
  void _handleDragDown(DragDownDetails details){
    localPosition = details.localPosition;
    downMicrosecond = DateTime.now().millisecond;
    // print('_handleDragDown');
    // print(details.globalPosition.dx);
    // print(downMicrosecond);
  }

  void _handleDragStart(DragStartDetails details){
    // 获取对应可见的subpage=
    currentDragStartDetails = details;
    if(subPageController.hasClients){
       double dx = (localPosition?.dx) as double;
       if(subPageController?.page?.round() == 2) {
         // print('_handleDragStart');
         // print(details.localPosition.dx);
         // print( details.kind);
         // print( '-------');
         if (dx - details.localPosition.dx > 0) {
           _drag = pageController?.position.drag(details, _disposeDrag);
         } else if(dx - details.localPosition.dx < 0){
           _drag = subPageController?.position.drag(details, _disposeDrag);
         } else {
           _drag = null;
         }
       }else{
         _drag = subPageController?.position.drag(details,_disposeDrag);
       }

    }else{
      _drag = pageController?.position.drag(details,_disposeDrag);
    }

  }

  void _handleDragUpdate(DragUpdateDetails details) {
    if(_drag==null){
      double? lastDx = lastDragUpdateDetails?.localPosition.dx;
      if(lastDx != null){
        if(lastDx - details.localPosition.dx < 0){
          _drag = pageController?.position.drag(currentDragStartDetails!,_disposeDrag);
        }else{
          _drag = subPageController?.position.drag(currentDragStartDetails!,_disposeDrag);
        }
      }
      lastDragUpdateDetails = details;
    }

    _drag?.update(details);
    // print('_handleDragUpdate');
    // print(details.localPosition.dx);
  }

  void _handleDragEnd(DragEndDetails details) {
    _drag?.end(details);
    // print('_handleDragEnd');
  }

  void _handleDragCancel() {
    _drag?.cancel();
    // print('_handleDragCancel');
  }

  ///拖拽结束了，释放  _drag
  void _disposeDrag() {
    _drag = null;
    // print('_disposeDrag');
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: ScreenUtil().screenHeight - 150.r,
      child: RawGestureDetector(
        gestures: <Type, GestureRecognizerFactory>{
          HorizontalDragGestureRecognizer:GestureRecognizerFactoryWithHandlers<HorizontalDragGestureRecognizer>(
                  ()=>HorizontalDragGestureRecognizer(),
                  (HorizontalDragGestureRecognizer instance){
                    instance
                      ..onDown = _handleDragDown
                      ..onStart = _handleDragStart
                      ..onUpdate = _handleDragUpdate
                      ..onEnd = _handleDragEnd
                      ..onCancel = _handleDragCancel;
                  }
          )
        },
        child: PageView(
          scrollDirection: Axis.horizontal,
          controller: pageController,
          physics: const NeverScrollableScrollPhysics(),
          children: [
            Column(
              children: [
                SizedBox(
                  width: ScreenUtil().screenWidth,
                  height: 30.r,
                  child:Row(
                    children: const [
                      Text('热门'),
                      Text('流行'),
                      Text('古风')
                    ],
                  ),
                ),
                SizedBox(
                  width: ScreenUtil().screenWidth,
                  height: ScreenUtil().screenHeight - 180.r ,
                  child: PageView(
                    physics: const NeverScrollableScrollPhysics(),
                    scrollDirection: Axis.horizontal,
                    controller: subPageController,
                    children: [
                      Container(
                        width: ScreenUtil().screenWidth,
                        height: ScreenUtil().screenHeight - 180.r,
                        color: Colors.red,
                        child: ListView.builder(
                          itemBuilder: (context, index) {
                            return  SizedBox(height: 30.r,child: Text('List Item $index'));
                          },
                          itemCount: 30,
                        ),
                      ),
                      Container(
                        width: ScreenUtil().screenWidth,
                        height: ScreenUtil().screenHeight - 180.r,
                        color: Colors.orange,
                        child: ListView.builder(
                          itemBuilder: (context, index) {
                            return  SizedBox(height: 30.r,child: Text('List Item $index'));
                          },
                          itemCount: 30,
                        ),
                      ),
                      Container(
                        width: ScreenUtil().screenWidth,
                        height: ScreenUtil().screenHeight - 180.r,
                        color: Colors.green,
                        child: ListView.builder(
                          itemBuilder: (context, index) {
                            return  SizedBox(height: 30.r,child: Text('List Item $index'));
                          },
                          itemCount: 30,
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
            Container(
              color: Colors.teal,
              child: const Center(
                child: Text("This is a page2"),
              ),
            ),
            Container(
              color: Colors.amber,
              child: const Center(
                child: Text("This is a page3"),
              ),
            ),
          ],
        )
      ),
    );
  }

}



